package cn.edu.nju.ws.sview;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import cn.edu.nju.ws.sview.rdf.ResourceFactory;

/**
 * set the configuration of sview
 * 
 * @author Saisai Gong
 *
 */
public class Config {
	private final static int HTTP_REDIRECT ;
	private final static String HTTP_REDIRECT_URL;
	
	static{
		Properties properties = new Properties();
		try {
			properties.load(ResourceFactory.class.getClassLoader().getResourceAsStream("conf.properties")) ;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HTTP_REDIRECT = Integer.parseInt(properties.getProperty("http_redirect"));
		HTTP_REDIRECT_URL =properties.getProperty("http_redirect_url");
		properties.clear();
	}
	
	/**
	 * get the html base href of the page
	 * If http redirect not used then http://servername:port/path/
	 * or use the http redirect url
	 * @return
	 */
    public static String getBaseHref(HttpServletRequest request){
    	String basePath = null;
    	if(Config.HTTP_REDIRECT==1){
    		basePath = getBaseByRedirectURL();
    	}else{
    		basePath = getBaseByRequest(request);
    	}
    	return basePath;
    }
    
    private static String getBaseByRequest(HttpServletRequest request){
    	String path = request.getContextPath();
    	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    	return basePath;
    }
    
    private static String getBaseByRedirectURL(){
    	String basePath = Config.HTTP_REDIRECT_URL;
    	return basePath;
    }
    
    private static String getBaseByRoot(){
    	String basePath = "/";
    	return basePath;
    }
}
